package cn.itcalvin.sncoal.cth.controller;

import cn.itcalvin.sncoal.cth.aspect.LogAnnotation;
import cn.itcalvin.sncoal.cth.aspect.RoleAnnotation;
import cn.itcalvin.sncoal.cth.pojo.common.ReqIdList;
import cn.itcalvin.sncoal.cth.pojo.common.ReqLong;
import cn.itcalvin.sncoal.cth.pojo.common.RespUserInfo;
import cn.itcalvin.sncoal.cth.pojo.common.RespUserInfoEx;
import cn.itcalvin.sncoal.cth.pojo.req.user.*;
import cn.itcalvin.sncoal.cth.pojo.response.user.RespUserList;
import cn.itcalvin.sncoal.cth.service.InfoUserService;
import cn.itcalvin.sncoal.cth.utils.JwtUtil;
import cn.itcalvin.vo.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/user")
public class UserController {


    @Resource
    InfoUserService infoUserService;

    @ApiOperation("用户列表")
    @PostMapping("list")
    @LogAnnotation(value = "用户列表")
    public Message<List<RespUserList>> list(@RequestBody @Valid ReqUserList reqUserList){
        final RespUserInfo userInfo = JwtUtil.getUserInfo();
        if(userInfo.getRoleType()==4){
            reqUserList.setMineId(userInfo.getBelongId());
        }
        return infoUserService.list(reqUserList);
    }


    @ApiOperation("新增用户")
    @PostMapping("add")
    @LogAnnotation(value = "新增用户")
    public Message add(@RequestBody @Valid ReqUserAdd reqUserAdd){
        return infoUserService.add(reqUserAdd);
    }


    @ApiOperation("修改用户")
    @PostMapping("modify")
    @LogAnnotation(value = "修改用户")
    public Message modify(@RequestBody @Valid ReqUserModify reqUserModify){
        return infoUserService.modify(reqUserModify);
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    @LogAnnotation(value = "删除用户")
    public Message delete(@RequestBody @Valid ReqLong reqLong){
        return infoUserService.delete(reqLong);
    }


    @ApiOperation(value = "获取个人信息（矿井用户需要返回评分密码）")
    @PostMapping("/getUserInfo")
    @LogAnnotation(value = "获取个人信息")
    public Message<RespUserInfoEx> getUserInfo(){
        final RespUserInfo userInfo = JwtUtil.getUserInfo();
        return  infoUserService.getUserInfo(userInfo.getId());
    }

    @ApiOperation(value = "修改登录密码")
    @PostMapping("/modifyLoginPwd")
    @LogAnnotation(value = "修改登录密码")
    public Message<RespUserInfo> modifyLoginPwd(@RequestBody @Valid ReqModifyLoginPwd reqModifyLoginPwd){
        final RespUserInfo userInfo = JwtUtil.getUserInfo();
        return  infoUserService.modifyLoginPwd(userInfo.getId(),reqModifyLoginPwd);
    }

    @PostMapping("/resetLoginPassword")
    @ApiOperation(value = "重置登录密码")
    @LogAnnotation(value = "重置登录密码")
    public Message resetLoginPassword(@RequestBody @Valid ReqIdList reqIdList){
        return infoUserService.resetLoginPassword(reqIdList);
    }

    @ApiOperation("修改用户状态")
    @PostMapping("modifyState")
    @LogAnnotation(value = "修改用户状态")
    public Message modifyState(@RequestBody @Valid ReqUserModifyState reqUserModifyState){
        return infoUserService.modifyState(reqUserModifyState);
    }

}
